/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.web.time;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.I18nUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.source.entity.time.EleSourceDate;
import com.thinkgem.jeesite.modules.source.service.time.EleSourceDateService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 日期资源维护Controller
 * @author ZC
 * @version 2017-04-19
 */
@Controller
@RequestMapping(value = "${adminPath}/source/time/eleSourceDate")
public class EleSourceDateController extends BaseController {

	@Autowired
	private EleSourceDateService eleSourceDateService;
	@Autowired
	private SystemService systemService;
	@ModelAttribute
	public EleSourceDate get(@RequestParam(required=false) String id) {
		EleSourceDate entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleSourceDateService.get(id);
		}
		if (entity == null){
			entity = new EleSourceDate();
		}
		return entity;
	}
	
	@RequiresPermissions("source:time:eleSourceDate:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleSourceDate eleSourceDate, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		List<User> list = null;
		if(user.isAdmin()) {
			list = systemService.findAllUsers(user);
		}else {
			list = systemService.findAllUsersInCompany(user);
			list.add(UserUtils.getAdminUser());		
		}		
		eleSourceDate.setUsers(list);
		Page<EleSourceDate> page = eleSourceDateService.findPage(new Page<EleSourceDate>(request, response), eleSourceDate); 
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		page.setLocale(locale);
		model.addAttribute("page", page);
		return "modules/source/time/eleSourceDateList";
	}

	@RequiresPermissions("source:time:eleSourceDate:view")
	@RequestMapping(value = "form")
	public String form(EleSourceDate eleSourceDate, Model model,HttpServletRequest request) {
		model.addAttribute("eleSourceDate", eleSourceDate);
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		model.addAttribute("language", locale.getLanguage());
		return "modules/source/time/eleSourceDateForm";
	}

	@RequiresPermissions("source:time:eleSourceDate:edit")
	@RequestMapping(value = "save")
	public String save(EleSourceDate eleSourceDate, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, eleSourceDate)){
			return form(eleSourceDate, model,request);
		}
		eleSourceDateService.save(eleSourceDate);
		String locale = I18nUtils.getLocaleStr();
		String msg = "保存日期资源成功";
		if(Global.US.equals(locale)){
			msg = "date resource saved";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/time/eleSourceDate/?repage";
	}
	
	@RequiresPermissions("source:time:eleSourceDate:edit")
	@RequestMapping(value = "delete")
	public String delete(EleSourceDate eleSourceDate, RedirectAttributes redirectAttributes) {
		eleSourceDateService.delete(eleSourceDate);
		String locale = I18nUtils.getLocaleStr();
		String msg = "删除日期资源成功";
		if(Global.US.equals(locale)){
			msg = "Date resource saved";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/source/time/eleSourceDate/?repage";
	}

	@RequiresPermissions("source:time:eleSourceDate:check")
	@RequestMapping(value = "check")
	public String check(EleSourceDate eleSourceDate, RedirectAttributes redirectAttributes) {
		eleSourceDateService.check(eleSourceDate);
		addMessage(redirectAttributes, "审核日期资源成功");
		return "redirect:"+Global.getAdminPath()+"/source/time/eleSourceDate/?repage";
	}


}